package com.transsion.testing.callstate;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * Created by hunan.liu on 2017/7/10.
 */

public class phoneservice extends Service {
    @Nullable
    private void listenBroadcast(){
        IntentFilter phonestateFilter=new IntentFilter();
        phonestateFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");
        phonestateFilter.addAction("android.intent.action.PHONE_STATE");
        registerReceiver(phoneStateReceiver,phonestateFilter);
    }

    private BroadcastReceiver phoneStateReceiver=new BroadcastReceiver() {
        private static final String TAG="liuhunan";
        private  boolean incomingFlag=false;
        private  String incomingNumber=null;


        @Override
        public void onReceive(Context context, Intent intent) {
           // 如果是去电
            if(intent.getAction().equals(intent.ACTION_NEW_OUTGOING_CALL)){
               NotificationCompat.Builder builder=new NotificationCompat.Builder(phoneservice.this)
                      .setSmallIcon(R.mipmap.ic_launcher)
                       .setContentTitle("通话状态")
                      .setContentText("现在的通话状态为来电ing");
               Notification notification=builder.build();
              NotificationManager notificationManager=(NotificationManager)context.getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(1,notification);
                incomingFlag=false;
                String callNumber=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                Log.i(TAG,"是否来电："+ incomingFlag+" 通话状态为呼出, "+" 号码为： "+callNumber );   }
            //如果是来电
            else{
                TelephonyManager telephonyManager= (TelephonyManager) context.getSystemService
                        (Service.TELEPHONY_SERVICE);


                switch (telephonyManager.getCallState()){
                    case TelephonyManager.CALL_STATE_RINGING:
                        incomingFlag=true;
                        incomingNumber=intent.getStringExtra("incoming_number");
                        Log.i(TAG,"是否来电： "+ incomingFlag+" 通话状态为来电ing "+" 来电号码为： "+incomingNumber );



                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        if(incomingFlag){


                            Log.i(TAG,"是否来电： "+ incomingFlag+" 通话状态为通话ing "+" 通话号码为： "+incomingNumber );
                        }


                        break;
                    case TelephonyManager.CALL_STATE_IDLE:
                        Log.i(TAG,"是否来电： "+ incomingFlag+" 通话状态为空闲状态 "+" 号码为： "+null );
                        break;


                }
            }
        }

        };

    public IBinder onBind(Intent intent) {

        return null;
    }
}
